package com.amphiesma.gwips;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Looper;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {

	private LocationListener locationListener;
  	private LocationManager locationManager;
  	private Spinner spinnerAnimalType;
  	private ArrayAdapter<CharSequence> adapter;
  	private DBHandler dbHandler;
  	private double latitude, longitude, altitude=-15000, accuracy;
  	long utcTime;
  	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinnerAnimalType = (Spinner) findViewById(R.id.spinner_type);
        adapter = ArrayAdapter.createFromResource(this, 
        		R.array.animal_types_array, android.R.layout.simple_spinner_dropdown_item);
        dbHandler = new DBHandler(this);
        spinnerAnimalType.setAdapter(adapter);
        setupLocation();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void setupLocation() {
    	// Define a listener that responds to location updates
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    	locationListener = new LocationListener() {
    	    public void onLocationChanged(Location location) {
    	    	insertRow(location);
    	    }

    	    public void onStatusChanged(String provider, int status, Bundle extras) {}

    	    public void onProviderEnabled(String provider) {
    	    }

    	    public void onProviderDisabled(String provider) {
	    		Toast toast = Toast.makeText(getApplicationContext(), R.string.disabled_location, Toast.LENGTH_LONG);
    	    	toast.show();
    	    }
    	  };

    }
    
    public void insertRow(Location location) {
    	Spinner spinnerType = (Spinner) findViewById(R.id.spinner_type);
    	
    	EditText editCommonName = (EditText) findViewById(R.id.edit_common_name);
    	EditText editGenusName = (EditText) findViewById(R.id.edit_genus);
    	EditText editSpeciesName = (EditText) findViewById(R.id.edit_species);
    	EditText editComments = (EditText) findViewById(R.id.edit_comments);
    	
    	
    	latitude = location.getLatitude();
    	longitude = location.getLongitude();
    	if(location.hasAltitude())
    		altitude = location.getAltitude();
    	else
    		altitude = -15000;    	    	
    	accuracy = location.getAccuracy();
    	
    	String type = (String) spinnerType.getSelectedItem();
    	String commonName = editCommonName.getText().toString();
    	String genusName = editGenusName.getText().toString();
    	String speciesName = editSpeciesName.getText().toString();
    	String comments = editComments.getText().toString();	
    	
    	AnimalEntry entry = new AnimalEntry(type, commonName, genusName, speciesName, comments,
    			latitude, longitude, altitude, accuracy, System.currentTimeMillis());
    	
    	dbHandler.insertRow(entry);
    }
    
	public void getLocation(View view)
    {
    	locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, locationListener, Looper.myLooper());
//		insertRow(locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER));
    }    
	
	public void copyData(View view) {
		try {
			dbHandler.exportDB();
    		Toast toast = Toast.makeText(getApplicationContext(), R.string.copy_success, Toast.LENGTH_LONG);
    		toast.show();
		}
		catch(FileNotFoundException e) {
			Log.e("gwips:export", ""+e);
		}	
		catch(IOException e) {
			Log.e("gwips:export", ""+e);
		}
	}
}
